using CollabApp.Domain.Entities.Game;

namespace CollabApp.Domain.Services.Game;

/// <summary>
/// 特殊事件服务接口
/// 负责处理游戏中的特殊事件，如重力反转、时间加速等
/// 每局游戏有10%概率触发一个随机特殊事件
/// </summary>
public interface ISpecialEventService
{
    /// <summary>
    /// 检查是否应该触发特殊事件
    /// </summary>
    /// <param name="gameId">游戏ID</param>
    /// <returns>是否触发特殊事件</returns>
    Task<bool> ShouldTriggerSpecialEventAsync(Guid gameId);

    /// <summary>
    /// 触发随机特殊事件
    /// </summary>
    /// <param name="gameId">游戏ID</param>
    /// <returns>触发的特殊事件结果</returns>
    Task<SpecialEventResult> TriggerRandomEventAsync(Guid gameId);

    /// <summary>
    /// 触发指定特殊事件
    /// </summary>
    /// <param name="gameId">游戏ID</param>
    /// <param name="eventType">事件类型</param>
    /// <returns>特殊事件结果</returns>
    Task<SpecialEventResult> TriggerEventAsync(Guid gameId, SpecialEventType eventType);

    /// <summary>
    /// 获取当前活跃的特殊事件
    /// </summary>
    /// <param name="gameId">游戏ID</param>
    /// <returns>当前活跃事件列表</returns>
    Task<List<ActiveSpecialEvent>> GetActiveEventsAsync(Guid gameId);

    /// <summary>
    /// 更新特殊事件状态
    /// </summary>
    /// <param name="gameId">游戏ID</param>
    /// <returns>更新结果</returns>
    Task<EventUpdateResult> UpdateEventStatusAsync(Guid gameId);

    /// <summary>
    /// 结束指定特殊事件
    /// </summary>
    /// <param name="gameId">游戏ID</param>
    /// <param name="eventId">事件ID</param>
    /// <returns>结束结果</returns>
    Task<bool> EndEventAsync(Guid gameId, string eventId);
}

/// <summary>
/// 特殊事件类型枚举
/// </summary>
public enum SpecialEventType
{
    /// <summary>
    /// 重力反转 - 移动方向反转，持续20秒
    /// </summary>
    GravityReverse = 1,
    
    /// <summary>
    /// 时间加速 - 移动速度翻倍但难控制，持续15秒
    /// </summary>
    TimeAcceleration = 2,
    
    /// <summary>
    /// 道具雨 - 瞬间刷新8-12个随机道具
    /// </summary>
    PowerUpRain = 3,
    
    /// <summary>
    /// 领地震动 - 所有领地边界随机波动，持续10秒
    /// </summary>
    TerritoryQuake = 4,
    
    /// <summary>
    /// 透明模式 - 所有轨迹半透明，持续30秒
    /// </summary>
    InvisibilityMode = 5
}

/// <summary>
/// 特殊事件结果
/// </summary>
public class SpecialEventResult
{
    /// <summary>
    /// 操作是否成功
    /// </summary>
    public bool Success { get; set; }
    
    /// <summary>
    /// 事件唯一ID
    /// </summary>
    public string EventId { get; set; } = string.Empty;
    
    /// <summary>
    /// 事件类型
    /// </summary>
    public SpecialEventType EventType { get; set; }
    
    /// <summary>
    /// 事件名称
    /// </summary>
    public string EventName { get; set; } = string.Empty;
    
    /// <summary>
    /// 事件描述
    /// </summary>
    public string Description { get; set; } = string.Empty;
    
    /// <summary>
    /// 开始时间
    /// </summary>
    public DateTime StartTime { get; set; }
    
    /// <summary>
    /// 持续时间
    /// </summary>
    public TimeSpan Duration { get; set; }
    
    /// <summary>
    /// 结束时间
    /// </summary>
    public DateTime EndTime { get; set; }
    
    /// <summary>
    /// 受影响的玩家ID列表
    /// </summary>
    public List<Guid> AffectedPlayerIds { get; set; } = new();
    
    /// <summary>
    /// 事件参数
    /// </summary>
    public Dictionary<string, object> EventParameters { get; set; } = new();
    
    /// <summary>
    /// 错误信息
    /// </summary>
    public List<string> Errors { get; set; } = new();
    
    /// <summary>
    /// 提示信息
    /// </summary>
    public List<string> Messages { get; set; } = new();
}

/// <summary>
/// 活跃特殊事件
/// </summary>
public class ActiveSpecialEvent
{
    /// <summary>
    /// 事件ID
    /// </summary>
    public string EventId { get; set; } = string.Empty;
    
    /// <summary>
    /// 事件类型
    /// </summary>
    public SpecialEventType EventType { get; set; }
    
    /// <summary>
    /// 事件名称
    /// </summary>
    public string EventName { get; set; } = string.Empty;
    
    /// <summary>
    /// 开始时间
    /// </summary>
    public DateTime StartTime { get; set; }
    
    /// <summary>
    /// 剩余时间（秒）
    /// </summary>
    public int RemainingSeconds { get; set; }
    
    /// <summary>
    /// 是否活跃
    /// </summary>
    public bool IsActive { get; set; }
    
    /// <summary>
    /// 事件进度（0-1）
    /// </summary>
    public float Progress { get; set; }
    
    /// <summary>
    /// 事件参数
    /// </summary>
    public Dictionary<string, object> Parameters { get; set; } = new();
}

/// <summary>
/// 事件更新结果
/// </summary>
public class EventUpdateResult
{
    /// <summary>
    /// 已完成的事件列表
    /// </summary>
    public List<string> CompletedEvents { get; set; } = new();
    
    /// <summary>
    /// 仍在进行的事件列表
    /// </summary>
    public List<ActiveSpecialEvent> ActiveEvents { get; set; } = new();
    
    /// <summary>
    /// 更新时间
    /// </summary>
    public DateTime UpdateTime { get; set; } = DateTime.UtcNow;
}
